import 'package:flutter/material.dart';
import 'package:bruno/bruno.dart';
import 'package:miuu_note_app/page/my_page.dart';
import 'package:miuu_note_app/page/snippet_page.dart';
import 'diary_page.dart';
import 'home_page.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({super.key});

  @override
  State<IndexPage> createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  late PageController _pageController;
  int _currentPage = 0;

  var children = <Widget>[
    KeepAliveView(keep_alive: true, child: HomePage()),
    KeepAliveView(keep_alive: true, child: SnippetPage()),
    KeepAliveView(keep_alive: true, child: DiaryPage()),
    KeepAliveView(keep_alive: true, child: MyPage())
  ];

  var titles = ['待办', '随笔', '日记', '我的'];

  late List<BrnBottomTabBarItem> bottomBar = <BrnBottomTabBarItem>[
    BrnBottomTabBarItem(
        icon: const Image(image: AssetImage("assets/icons/navbar_house.png")),
        activeIcon:
            const Image(image: AssetImage("assets/icons/navbar_house.png")),
        title: Text(titles[0])),
    BrnBottomTabBarItem(
        icon: const Image(image: AssetImage("assets/icons/navbar_house.png")),
        title: Text(titles[1])),
    BrnBottomTabBarItem(
        icon: const Image(image: AssetImage("assets/icons/navbar_house.png")),
        title: Text(titles[2])),
    BrnBottomTabBarItem(
        icon: const Image(image: AssetImage("assets/icons/navbar_house.png")),
        activeIcon:
            const Image(image: AssetImage("assets/icons/navbar_house.png")),
        title: Text(titles[3])),
  ];

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: 0, keepPage: true);
  }

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: PageView(
              onPageChanged: (pagePos) {
                setState(() {
                  _currentPage = pagePos;
                });
              },
              physics: const NeverScrollableScrollPhysics(),
              scrollDirection: Axis.horizontal,
              children: children,
              controller: _pageController,
            ),
          ),
          BrnBottomTabBar(
            fixedColor: Colors.blue,
            currentIndex: _currentPage,
            onTap: (value) {
              setState(() {
                _currentPage = value;
                _pageController.jumpToPage(_currentPage);
              });
            },
            badgeColor: Colors.red,
            items: bottomBar,
          )
        ],
      ),
    );
  }
}

class KeepAliveView extends StatefulWidget {
  bool keep_alive;
  Widget child;
  KeepAliveView({super.key, required this.keep_alive, required this.child});

  @override
  State<KeepAliveView> createState() => _KeepAliveViewState();
}

class _KeepAliveViewState extends State<KeepAliveView>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    return widget.child;
  }

  @override
  bool get wantKeepAlive => widget.keep_alive; // 是否需要缓存
}
